package domain;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import domain.PhoneNumber.PhoneType;

public class DAOContact {
	private Session session = null;

	private void createSession() {
		Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
		SessionFactory sessionFactory = cfg.buildSessionFactory();
		this.session = sessionFactory.getCurrentSession();
	}
	
	public void addContact(String firstName, String lastName,
			String email, String street, String city, String zip,
			String country, String homePhone, String cellPhone,
			String officePhone) 
	{
		Session session = null;
		Contact contact = null;
		Address address = null;
		PhoneNumber hPhone = null;
		PhoneNumber cPhone = null;
		PhoneNumber oPhone = null;
		
		try
		{
			SessionFactory sessionFactory =
				new Configuration().configure().buildSessionFactory();
			session = sessionFactory.openSession();
			org.hibernate.Transaction tx = session.beginTransaction();
			//create a contact and save it into the DB
			contact = new Contact(firstName, lastName, email);
			
			address = new Address(street, city, zip, country);
			contact.setAddress(address); // Uni birectionnel
			
			if (homePhone != null)
			{
				hPhone = new PhoneNumber(PhoneType.HOME_PHONE, homePhone);
				contact.getPhoneNumbers().add(hPhone);
				hPhone.setContact(contact);
			}
			if (cellPhone != null)
			{
				cPhone = new PhoneNumber(PhoneType.CELL_PHONE, cellPhone);
				contact.getPhoneNumbers().add(cPhone);
				cPhone.setContact(contact);
			}
			if (officePhone != null)
			{
				oPhone = new PhoneNumber(PhoneType.OFFICE_PHONE, officePhone);
				contact.getPhoneNumbers().add(oPhone);
				oPhone.setContact(contact);
			}
						
			//save the contact into the DB
			//session.save(a);
			session.save(contact); // or session.persist(contact);
			
			//if you modify one of its properties, no need to save it again
			//contact.setFirstName("Robin");
			//mandatory to flush the data into the DB
			tx.commit();
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
	}

	public void addContact(String firstName, String lastName, String email)
	{
		Session session = null;
		Address a = null;
		try
		{
			SessionFactory sessionFactory =
				new Configuration().configure().buildSessionFactory();
			session = sessionFactory.openSession();
			org.hibernate.Transaction tx = session.beginTransaction();
			//create a contact and save it into the DB
			Contact contact = new Contact(firstName, lastName, email);
			a = new Address("street", "city", "zip", "country");
			contact.setAddress(a);
			//save the contact into the DB		this.idContact = id;
			//session.save(a);
			session.save(contact); // or session.persist(contact);
			
			//if you modify one of its properties, no need to save it again
			//contact.setFirstName("Robin");
			//mandatory to flush the data into the DB
			tx.commit();
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
	}
	public void addContact() {
		// Session session = null;
		// try{
		// SessionFactory sessionFactory =
		// new Configuration().configure().buildSessionFactory();
		// session = sessionFactory.openSession();
		// //begin a transaction
		// org.hibernate.Transaction tx = session.beginTransaction();
		// //create a contact and save it into the DB
		// Contact contact = new Contact();
		// contact.setId(1);
		// contact.setFirstName("Robbie");
		// //save the contact into the DB
		// session.save(contact); // or session.persist(contact);
		// //if you modify one of its properties, no need to save it again
		// contact.setFirstName("Robin");
		// //mandatory to flush the data into the DB
		// tx.commit();
		// }catch(Exception e){
		// System.out.println(e.getMessage());
		// }

		Session session = null;
		try {
			SessionFactory sessionFactory = new Configuration().configure()
					.buildSessionFactory();
			session = sessionFactory.openSession();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
	 public static void main(String[] args)
	 {
		 DAOContact daoContact = new DAOContact();
//		 String firstName = "aAA";
//		 String lastName = "b";
//		 String email ="c@gmail.com";
//		 String street = "street";
//		 String city = "city";
//		 String zip = "zip";
//		 String country = "country";
//		 String homePhone = "homePhone";
//		 String cellPhone = "cellPhone";
//		 String officePhone = "oPhone";
		 
//		 daoContact.addContact(firstName, 
//				 lastName, 
//				 email, 
//				 street, 
//				 city, 
//				 zip, 
//				 country, 
//				 homePhone, 
//				 cellPhone, 
//				 officePhone);
		 
		 
		 daoContact.removeContact(4);
	 }
	 public void removeContact(long contactId)
		{
			Session session = null;
			Address a = null;
			try
			{
				SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
				session = sessionFactory.openSession();
				Transaction tx = session.beginTransaction();
				Contact contact = (Contact) session.load(Contact.class, contactId);
				session.delete(contact);
				tx.commit();
			}catch(Exception e){
				System.out.println(e.getMessage());
			}
		}
}
